// play 
const app = getApp()
const url = app.basicsUrl();

Page({
  data: {
    winWidth: 0,
    winHeight: 0,
    score: 0,
    setInter: 0,
    setInterstop: 0,
    dIdx: '',
    hongbaoId: '',
    gameId: '',
    challengeNum: "1",
    clock: '',
    num: 19,
    end: false,
    start: false,
    offplay: true,
    offplaysuccess: true,
    blockData: [],
    tokenKey: '',
    token: '',
    openId: '',
    dataid: '',
    hide: 1,
    animationData: '',
    list: [
      {
        select: 0
      }, {
        select: 1
      }, {
        select: 2
      }, {
        select: 3
      },
    ]
  },

  onReady: function () {
    var array = [];
    // 先生成一个10个长度的数组
    for (var i = 0; i < 10; i++) {
      // 生成一个随机位数为1的数组
      var orderArray = [0, 0, 0, 0];
      var randomNum = Math.floor(Math.random() * 4);
      orderArray[randomNum] = 1;
      array.push({ id: i, block: orderArray });
    }
    this.setData({
      blockData: array.reverse()
    });
  },

  bindforget: function () {
    var that = this;
    wx.redirectTo({
      url: '/pages/games/games?hongBaoHardType=' + that.data.dIdx + '&gameId=' + that.data.gameId + '&hongbaoId=' + that.data.hongbaoId
    });
  },

  bindagain: function (e) {
    var that = this;
    that.setData({
      challengeNum: parseInt(e.currentTarget.dataset.challengenum) + 1
    });
    wx.redirectTo({
      url: '/pages/play/play?hongBaoHardType=' + that.data.dIdx + '&challengeNum=' + that.data.challengeNum + '&hongbaoId=' + that.data.hongbaoId
    });
  },

  bindstart: function () {
    var that = this;
    that.timeInterval();
    that.setData({
      start: true
    });
    that.startgame()
  },

  startgame: function () {
    var that = this;
    var params = {};
    params['hongBaoId'] = that.data.hongbaoId,
      params['tokenKey'] = that.data.tokenKey,
      params['token'] = that.data.token,
      params['openId'] = that.data.openId,
      wx.request({
        url: url + "authorized/startGame",
        data: params,
        header: {
          'content-type': 'application/json'
        },
        success: res => {
          if (res.data.code == 200) {
            that.setData({
              dataid: res.data.data.id
            });
          } else {
            wx.showLoading({
              title: res.data.msg,
            })
            setTimeout(function () {
              wx.hideLoading()
            }, 1000)
          }
        }
      })
  },

  gamesuccess: function () {
    var that = this;
    var params = {};
    params['hongBaoId'] = that.data.hongbaoId,
      params['gameId'] = that.data.dataid,
      params['tokenKey'] = that.data.tokenKey,
      params['token'] = that.data.token,
      params['openId'] = that.data.openId,
      wx.request({
        url: url + "authorized/pickHongBao",
        data: params,
        header: {
          'content-type': 'application/json'
        },
        success: res => {
          if (res.data.code == 200) {
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/games/games?msg=' + res.data.data.msg + '&hasPickMoney=' + res.data.data.hasPickMoney + '&money=' + res.data.data.money + '&hongbaoId=' + that.data.hongbaoId + '&gameId=' + that.data.gameId + '&hongBaoHardType=' + that.data.dIdx
              })
            }, 1000)
          } else {
            wx.showLoading({
              title: res.data.msg,
            })
            setTimeout(function () {
              wx.hideLoading()
            }, 1000)
          }
        }
      })
  },

  bgshow:function(){
    var that = this;
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })
    that.animation = animation
    that.animation.scale(2, 2).step()
    that.setData({
      animationData: animation.export()
    });
  },

  handleClick: function (events) {
    var that = this;
    var id = events.currentTarget.id;
    var index = events.currentTarget.dataset.index;
    // if (that.data.hide == index) {
    //   that.setData({
    //     hide: -1,
    //   })
    // } else {
    //   that.setData({
    //     hide: index,
    //   })
    // }
    that.setData({
      hide: index,
    })
    var line = id.split("-")[1];
    var column = id.split("-")[2];
    var isBlack = id.split("-")[3];
    var blockData = that.data.blockData.reverse();
    var dIdx = that.data.dIdx;
    var setInter = that.data.setInter / 10;
    var score = that.data.score;
    var orderArray = [0, 0, 0, 0];
    // 判断是否是第一行
    if (line != blockData[0].id) {
      that.setData({
        offplay: false,
        end: true,
        start: true
      });
      that.bgshow()
      that.startgame()
    }
    // 判断是否正确
    if (isBlack != 1) {
      that.setData({
        offplay: false,
        end: true,
        start: true
      });
      that.bgshow()
      that.startgame()
    }
    // 判断是否完成挑战
    if (dIdx == 'superHard' && score == 18) {
      if (setInter <= 6) {
        that.setData({
          offplaysuccess: false,
          end: true,
          start: true
        });
        that.gamesuccess()
      } else {
        that.setData({
          offplay: false,
          end: true,
          start: true
        });
      }
      that.bgshow()
    } else if (dIdx == 'hard' && score == 18) {
      if (setInter <= 7) {
        that.setData({
          offplaysuccess: false,
          end: true,
          start: true
        });
        that.gamesuccess()
      } else {
        that.setData({
          offplay: false,
          end: true,
          start: true
        });
      }
      that.bgshow()
    } else if (dIdx == 'common' && score == 18) {
      if (setInter <= 8) {
        that.setData({
          offplaysuccess: false,
          end: true,
          start: true
        });
        that.gamesuccess()
      } else {
        that.setData({
          offplay: false,
          end: true,
          start: true
        });
      }
      that.bgshow()
    } else if (dIdx == 'easy' && score == 18) {
      if (setInter <= 9) {
        that.setData({
          offplaysuccess: false,
          end: true,
          start: true
        });
        that.gamesuccess()
      } else {
        that.setData({
          offplay: false,
          end: true,
          start: true
        });
      }
      that.bgshow()
    }
    // 正确下一个
    // 分数++
    score++;
    orderArray[Math.floor(Math.random() * 4)] = 1;
    blockData.push({ id: score + 10, block: orderArray });
    blockData.shift();
    this.setData({
      silding: true,
      score: score,
      blockData: blockData.reverse()
    });
  },

  timeInterval: function () {
    var that = this;
    var i = 0;
    var t = setInterval(function () {
      that.setData({
        setInter: i
      });
      i++;
      if (that.data.offplay == false) {
        clearInterval(t);
        that.setData({
          setInterstop: that.data.setInter/10
        });
      }
    }, 100)
    
  },

  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    wx.getStorage({
      key: 'loginInfo',
      success: function (res) {
        that.setData({
          tokenKey: res.data.id,
          token: res.data.token,
          openId: res.data.openId,
        })
      }
    })
    that.setData({
      dIdx: options.hongBaoHardType,
      hongbaoId: options.hongbaoId,
      gameId: options.gameId,
      challengeNum: options.challengeNum
    });
    if (that.data.challengeNum === undefined) {
      that.setData({
        challengeNum: 1
      });
    }
    if (that.data.dIdx == 'easy') {
      that.setData({
        clock: 9,
      });
    } else if (that.data.dIdx == 'common') {
      that.setData({
        clock: 8,
      });
    } else if (that.data.dIdx == 'hard') {
      that.setData({
        clock: 7,
      });
    } else if (that.data.dIdx == 'superHard') {
      that.setData({
        clock: 6,
      });
    }
  }
})